<?php @session_start();?>
<?php
require_once('./safe.php');
require_once('../admin/config.php');
$conn = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname,$conn);
$sql="select * from ph_user where user_name='".$_SESSION["username"]."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$sqltdk="select * from ph_tdk where 1=1";
$resulttdk=mysql_query($sqltdk);
$rowtdk=mysql_fetch_array($resulttdk);
function check($str){
    return str_replace("'","''",$str);
}

$Status=check($_GET["Status"]);

$sqla="select * from ph_order where userid='".$_SESSION["userid"]."'";
IF(!empty($Status)){$sqla.=" and Status='".$Status."'";}
$resulta=mysql_query($sqla);

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

                    <link rel="apple-touch-icon" sizes="180x180" href="/images/iosinstalls/favicon/apple-touch-icon.png">

            <meta name="theme-color" content="#ffffff">

        <title><?php echo $rowtdk["sitename"];?> - Dashboard</title>

        <link href="../css/customer-iosinstalls.css?id=a9a9c5930bcdb195e9ee" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="<?php if(!empty($rowtdk["fontlink"])){echo '//fonts.googleapis.com/css?family='.$rowtdk["fontlink"];}else{echo "//fonts.googleapis.com/css?family=Roboto:300,400,500";}?>">
        <style type="text/css">
            body{font-family: '<?php echo $rowtdk["fontlink"];?>';}
        </style>
    </head>

    <body class="app header-fixed sidebar-fixed aside-menu-fixed aside-menu-hidden">
    <?php include("head.php");?> 
    <?php include("left.php");?>

            <!-- Main content -->
            <main class="main">
                    <div class="container-fluid padding-top">
        <div class="row">
            
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-primary">
                        <b>Orders</b>
                    </div>
                    <div class="card-body">
                        <ul class="nav nav-tabs" role="tablist">
                            <li class="nav-item">
                                <a class="nav-link <?php if(empty($Status)){echo "active";}?>" href="/dashboard/my-orders.php" role="tab" >
                                    All Orders
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link <?php if($Status=="Pending"){echo "active";}?>"  href="/dashboard/my-orders.php?Status=Pending" role="tab" >
                                    Pending Orders
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link <?php if($Status=="Active"){echo "active";}?>"  href="/dashboard/my-orders.php?Status=Active" role="tab" >
                                    Active Orders
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link <?php if($Status=="Completed"){echo "active";}?>"  href="/dashboard/my-orders.php?Status=Completed" role="tab">
                                    Completed Orders
                                </a>
                            </li>
                        </ul>
                        <div class="alert alert-info">
                            All orders with a keyword, will have its keyword automatically updated for the next 30 days after placing it.
                        </div>
                        <div id="errors" class="alert alert-danger hidden"></div>

                        <div class="float-right mb-3 d-none" id="optionButtons">
                            <button class="btn btn-success" id="copyOrders">Copy orders</button>
                            <button class="btn btn-warning" id="cancelOrders">Cancel orders</button>
                        </div>
                        
                        <div class="table-responsive">
                            <table class="table table-hover table-sm order-table mb-0">
                                <thead class="thead-default">
                                <tr>
                                    <th>ID</th>
                                    <th></th>
                                    <th></th>
                                    <th>App</th>
                                    <th>App Type</th>
                                    <th>Country</th>
                                    <th>Keyword</th>
                                    <!--<th>Last rank (New rank)</th>-->
                                    <th>Installs</th>
                                    <th>Split</th>
                                    <!--<th>Ratings</th>
                                    <th>Reviews</th>-->
                                    <th>Price</th>
                                    <th>Status</th>
                                    <th>Order date</th>
                                    <th></th>
                                    <th>
                                        <div class="custom-control custom-checkbox">
                                            <input type="checkbox" class="custom-control-input" id="checkAll" autocomplete="off">
                                            <label class="custom-control-label" for="checkAll"></label>
                                        </div>
                                    </th>
                                </tr>
                                
                                <?php 
while($rowa=mysql_fetch_array($resulta)){
$sqlb="select * from ph_country where cid='".$rowa["Country"]."'";
$resultb=mysql_query($sqlb);
$rowb=mysql_fetch_array($resultb);
$sqlf="select * from ph_myapp where ID='".$rowa["App"]."'";
$resultf=mysql_query($sqlf);
$rowf=mysql_fetch_array($resultf);
    ?>
                                <tr>
                                    <td><?php echo $rowa["ID"];?></td>
                                    <td></td>
                                    <td></td>
                                    <td><?php echo $rowf["app_name"];?></td>
                                    <td><?php if($rowf["thetype"]=="apple"){echo "<img src='../images/stores/apple_black.png' width='15'>";}else{echo "<img src='../images/stores/android_black.png' width='15'>";}?></td>
                                    <td><?php echo $rowb["country"];?></td>
                                    <td><?php echo $rowa["Keyword"];?></td>
                                    <!---<td><?php echo $rowa["Last_rank"];?></td>-->
                                    <td><?php echo $rowa["Installs"];?></td>
                                    <td><?php echo $rowa["Split"];?></td>
                                    <!--<td><?php echo $rowa["Ratings"];?>-<?php echo $rowa["rating_stars"];?></td>
                                    <td><?php echo $rowa["Reviews"];?>-<?php echo $rowa["Reviews_stars"];?></td>-->
                                    <td><?php echo $rowa["Price"];?></td>
                                    <td><?php echo $rowa["Status"];?></td>
                                    <td><?php echo $rowa["Order_date"];?></td>
                                    <td></td>
                                    <td>
                                        
                                            <input type="checkbox" class="options-checkbox" value="<?php echo $rowa["ID"];?>">
                                            <label class="optionButtons"></label>
                                        
                                    </td>
                                </tr>
                              <?php }?>
                                </thead>
                                <tbody>
                                                                </tbody>
                            </table>
                        </div>

                        <div class="col-12">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

            </main>
        </div>

        <footer class="app-footer">
                            &copy; 2021 <?php echo $rowtdk["sitename"];?>. All Rights Reserved
                        <div class="pull-right">
                Server time: <?php echo date('d-m-Y H:i:s',time());?>
            </div>
        </footer>

        <!-- Bootstrap and necessary plugins -->
        <script src="/js/backend.js?id=27298b157affa1f728a9"></script>

        
        <script type="text/javascript">
        $(function() {
            var optionsChecked = []

            $('.cancel').click(function () {
                if (confirm('Are you sure you want to cancel this order?')) {
                    var submitLadda = Ladda.create(document.querySelector('button[data-order-id="' + $(this).data('order-id') + '"]'));
                    submitLadda.start();
                    $.ajax({
                        url: "/dashboard/cancel-order",
                        type: "POST",
                        data: {
                            "order_id": [$(this).data('order-id')]
                        },
                        success: function (response) {
                            submitLadda.stop();

                            if (response.success) {
                                location.reload();
                            } else {
                                $('#errors').removeClass('hidden');
                                $('#errors').text("Something went wrong, please reload the page");
                            }
                        },
                        error: function() {
                            $('#errors').removeClass('hidden');
                            $('#errors').text("Something went wrong, please reload the page");
                        }
                    });
                }
            });

            $('#checkAll').change(function() {
                if ($(this).prop('checked') === true) {
                    $('.options-checkbox').each(function() {
                        $(this).prop('checked', true)
                        optionsChecked.push($(this).val())
                    })
                } else {
                    $('.options-checkbox').each(function() {
                        $(this).prop('checked', false)
                        optionsChecked = []
                    })
                }

                if (optionsChecked.length > 0) {
                    $('#optionButtons').removeClass('d-none');
                } else {
                    $('#optionButtons').addClass('d-none');
                }
            })

            $('.options-checkbox').change(function() {
                let value = $(this).val()
                if (optionsChecked.indexOf(value) !== -1) {
                    optionsChecked.splice(optionsChecked.indexOf(value), 1)
                } else {
                    optionsChecked.push(value)
                }

                if (optionsChecked.length > 0) {
                    $('#optionButtons').removeClass('d-none');
                } else {
                    $('#optionButtons').addClass('d-none');
                }

                if (optionsChecked.length === $('.options-checkbox').length) {
                    $('#checkAll').prop('checked', true)
                } else {
                    $('#checkAll').prop('checked', false)
                }
            })

            $('#cancelOrders').click(() => {
                if (confirm('Are you sure you want to cancel all these orders?')) {
                    $('#cancelOrders, #copyOrders').attr('disabled', true)
                    $.ajax({
                        url: "/dashboard/cancel-order.php",
                        type: "POST",
                        data: {
                            "order_id": optionsChecked
                        },
                        success: (response) => {
                            location.reload()
                        },
                        error: () => {
                            location.reload()
                        }
                    });
                }
            })

            $('#copyOrders').click(() => {
                window.location.href = '/dashboard/new-order.php?copy=' + optionsChecked.join(',')
            })
        });
    </script>

                        </body>
</html>